package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("你好啊傻逼");
//        System.out.println("商品编号"+req.getParameter("id"));
        //先获取id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //根据id获取对应图书的信息
        Book book = bookService.queryBookById(id);
        //根据图书信息得到对应的CartItem对象
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用cart。addItem方法添加到购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }

        cart.addItem(cartItem);
        req.getSession().setAttribute("lastItem",cartItem.getName());

        String referer = req.getHeader("Referer");
        //重定向到购物车页面
        resp.sendRedirect(referer);

    }

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxaddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("你好啊傻逼");
//        System.out.println("商品编号"+req.getParameter("id"));
        //先获取id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //根据id获取对应图书的信息
        Book book = bookService.queryBookById(id);
        //根据图书信息得到对应的CartItem对象
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用cart。addItem方法添加到购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }

        cart.addItem(cartItem);
        req.getSession().setAttribute("lastItem",cartItem.getName());

        String referer = req.getHeader("Referer");
       //返回购物车中的商品总数量和最后一个添加的商品名称

        Map<String,Object> map=new HashMap<>();

        map.put("totalCount",cart.getTotalCount());
        map.put("lastName",cartItem.getName());

        Gson gson=new Gson();
        String resultMapJsonString = gson.toJson(map);
        resp.getWriter().write(resultMapJsonString);

    }

    /**
     * 删除对应的商品项
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据的id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //获取session中的Cart对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //判断cart是否为空
        if (cart != null) {
            cart.deleteItem(id);
            //删除成功之后重新重定向到原来删除的位置
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车中的数据
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //判断购物车中的数据是否为空
        if (cart != null) {
            //清空购物车
            cart.clear();
            //重现返回购物车页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取商品的id和商品的数量
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int count = WebUtils.parseInt(req.getParameter("count"), 0);
        //获得当前存在session的购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //判断是否为空值
        if(cart!=null){
            //更改数量
            cart.updateCount(id,count);
     /*       if(count==0){
                //如果更改为count为零则删除此本书
            deleteItem(id);
            }*/
            //再次重定向到原来的界面
            resp.sendRedirect(req.getHeader("Referer"));
        }

    }
}
